package DataBases;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.*;

import wwtp.ontology.Water;

import DataBases.WaterAnalysisObject.Quality;

public class DBWaterAnalysis {

	private static String path = ".\\src\\DataBases\\WaterAnalysis.txt";
	private static DBWaterAnalysis instance = null;
	private Vector<WaterAnalysisObject> dB;


	private DBWaterAnalysis(){
		dB = new Vector<WaterAnalysisObject>();
		init();
	}

	private void init(){
		WaterAnalysisObject item;
		StringTokenizer token;
		Propiedades prop;
		WaterAnalysisObject.Quality calidad;
		double physical, chemical, biological;
		BufferedReader reader;

		try {
			reader = new BufferedReader(new FileReader(new File(path)));
			String line = null;
			while((line = reader.readLine()) != null){
				token = new StringTokenizer(line,";");
				physical = new Double(token.nextToken());
				chemical = new Double(token.nextToken());
				biological = new Double(token.nextToken());
				prop = new Propiedades(physical,chemical,biological);
				calidad = Quality.valueOf(token.nextToken());
				item = new WaterAnalysisObject(prop,calidad);
				dB.add(item);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static DBWaterAnalysis getInstance(){
		if(instance==null){
			instance = new DBWaterAnalysis();
		}
		return instance;
	}

	public String getQualityOf(Water w) {
		
		float phy = w.getPhysical().getPercentaje();
		float bio = w.getBiological().getPercentaje();
		float che = w.getChemical().getPercentaje();		
		
		System.out.println("Quality of: "+w.getVolume().getLitres()+"L P: " + phy + " B: "+ bio + " C: " +che);
	
		WaterAnalysisObject aux;
		boolean cond1, cond2, cond3;
		Iterator<WaterAnalysisObject> itr = dB.iterator();
		while(itr.hasNext()){
			aux = itr.next();
			cond1 = aux.getPropiedades().getBiological() >= w.getBiological().getPercentaje();
			cond2 = aux.getPropiedades().getChemical() >= w.getChemical().getPercentaje();
			cond3 = aux.getPropiedades().getPhysical() >= w.getPhysical().getPercentaje();
			if(cond1 && cond2 && cond3){
				return aux.getCalidad().toString();
			}
		}
		return "Undefined";
	
	}
}
